﻿Imports System.Math
Partial Class Acct_Aankoop
  Inherits System.Web.UI.Page
  Protected Sub dsInvoices_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles dsInvoices.Init
    CType(sender, AccessDataSource).DataFile = MayaConfig.DataFile
  End Sub
  Protected Sub grdInvoices_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles grdInvoices.SelectedIndexChanged
    Dim Inv As New clsInvoice
    Dim SupAcct As New Acct_Account
    Dim thisSupplier As New clsSupplier
    Dim TotalAmountGBP, thisRate As Double
    Dim InvNr As Long
    'Selected Invoice is loaded.
    InvNr = grdInvoices.SelectedDataKey.Value
    Inv.Load(InvNr)
    'Next we get the Supplier Details
    thisSupplier.Load(Inv.SupplierId)
    'Get the accountancy Account for this clsSupplier.
    SupAcct.Load(Inv.SupplierId)
    'Get the Vat Rate for this Supplier and this clsInvoice date.
    thisRate = clsVatRate.getVatRate(thisSupplier.VatCode, Inv.InputDate)
    txtVatRate.Text = thisRate.ToString("#0.00")
    thisRate = thisRate / 100
    'Fill the fields for entry.
    lstSupplierAccounts.SelectedValue = SupAcct.AccountID
    TotalAmountGBP = Inv.AmountVATGBP + Inv.AmountNetGBP + Inv.TransportGBP


    txtNetAmount1.Text = ((TotalAmountGBP / (1 + thisRate)) - Inv.TransportGBP).ToString("#0.00")
    txtTransportAmount.Text = Inv.TransportGBP.ToString("#0.00")
    txtVatAmount.Text = (TotalAmountGBP - (TotalAmountGBP / (1 + thisRate))).ToString("#0.00")
    txtTotalAmount.Text = TotalAmountGBP.ToString("#0.00")
    If Inv.Currency = "€" Then
      'In this case a currency conversion rate is applied. So we show the rate that was used.
      lblEuroRate.Visible = True
      lblEuroRate.Text = "Rate = " & Inv.Eur.Rate.ToString("#0.000") & " (" & Inv.Eur.RateDate.ToShortDateString & ")"
    Else
      lblEuroRate.Visible = False
    End If
    txtTotalCredit.Text = TotalAmountGBP.ToString("#0.00")
    txtTotalDebit.Text = TotalAmountGBP.ToString("#0.00")
  End Sub
  Protected Sub btBookTransaction_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btBookTransaction.Click
    'Debit Purchases
    Dim Inv As New clsInvoice
    Dim InvNr As Long
    InvNr = grdInvoices.SelectedDataKey.Value
    Inv.Load(InvNr)

    Dim thisTrx As New Acct_Transaction
    With thisTrx
      .TrxDate = Inv.InvDate
      .AccountID = lstContraAccount1.SelectedValue
      .Currency = "£"
      .Debit = Round(CDbl(txtNetAmount1.Text), 2)
      .Description = "Inv " & InvNr & " (" & Inv.SupplierId & ")"
      .DocTable = "Invoices"
      .DocID = InvNr.ToString
      If .Debit <> 0 Then thisTrx.Save()
    End With

    If lstContraAccount2.SelectedValue <> "Select Account" Then
      thisTrx = New Acct_Transaction
      With thisTrx
        .TrxDate = Inv.InvDate
        .AccountID = lstContraAccount2.SelectedValue
        .Currency = "£"
        .Debit = Round(CDbl(txtNetAmount2.Text), 2)
        .Description = "Inv " & InvNr & " (" & Inv.SupplierId & ")"
        .DocTable = "Invoices"
        .DocID = InvNr.ToString
        If .Debit <> 0 Then thisTrx.Save()
      End With
    End If

    If lstContraAccount3.SelectedValue <> "Select Account" Then
      thisTrx = New Acct_Transaction
      With thisTrx
        .TrxDate = Inv.InvDate
        .AccountID = lstContraAccount3.SelectedValue
        .Currency = "£"
        .Debit = Round(CDbl(txtNetAmount3.Text), 2)
        .Description = "Inv " & InvNr & " (" & Inv.SupplierId & ")"
        .DocTable = "Invoices"
        .DocID = InvNr.ToString
        If .Debit <> 0 Then thisTrx.Save()
      End With
    End If

    'Debit VAT Receivable
    If txtVatAmount.Text <> "" And txtVatAmount.Text <> "0" Then
      thisTrx = New Acct_Transaction
      With thisTrx
        .TrxDate = Inv.InvDate
        .AccountID = 411000
        .Currency = "£"
        .Debit = Round(CDbl(txtVatAmount.Text), 2)
        .Description = "Inv " & InvNr & " (" & Inv.SupplierId & ")"
        .DocTable = "Invoices"
        .DocID = InvNr.ToString
        If .Debit <> 0 Then thisTrx.Save()
      End With
    End If

    'Debit Transport kosten
    If txtTransportAmount.Text <> "" And txtTransportAmount.Text <> "0" Then
      thisTrx = New Acct_Transaction
      With thisTrx
        .TrxDate = Inv.InvDate
        .AccountID = 616500
        .Currency = "£"
        .Debit = Round(CDbl(txtTransportAmount.Text), 2)
        .Description = "Inv " & InvNr & " (" & Inv.SupplierId & ")"
        .DocTable = "Invoices"
        .DocID = InvNr.ToString
        If .Debit <> 0 Then thisTrx.Save()
      End With
    End If

    'Credit Supplier Account Receivable
    thisTrx = New Acct_Transaction
    With thisTrx
      .TrxDate = Inv.InvDate
      .AccountID = lstSupplierAccounts.SelectedValue
      .Currency = "£"
      .Credit = Round(CDbl(txtTotalAmount.Text), 2)
      .Description = "Invoice " & InvNr
      .DocTable = "Invoices"
      .DocID = InvNr.ToString
      thisTrx.Save()
    End With

    Inv.SetInAccounts()
    grdInvoices.DataBind()
    grdInvoices.SelectedIndex = -1
    'Reset all textboxes.
    txtNetAmount1.Text = "0.00"
    txtNetAmount2.Text = "0.00"
    txtNetAmount3.Text = "0.00"
    txtTotalAmount.Text = "0.00"
    txtTotalCredit.Text = "0.00"
    txtTotalDebit.Text = "0.00"
    txtTransportAmount.Text = "0.00"
    txtVatAmount.Text = "0.00"
    txtVatRate.Text = "0.00"
  End Sub

  Protected Sub lstContraAccount_DataBound(ByVal sender As Object, ByVal e As System.EventArgs) Handles lstContraAccount2.DataBound, lstContraAccount3.DataBound
    CType(sender, DropDownList).Items.Insert(0, "Select Account")
  End Sub
End Class
